package org.xiaov.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.xiaov.entity.po.UserPo;
import org.xiaov.framework.constant.ApiR;
import org.xiaov.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 描述：
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/12/17 22:10
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Resource
    private UserService userService;

    @Value("${server.port}")
    private String port;

    /**
     * 测试
     *
     * @return String
     */
    @GetMapping("/test")
    public String test() {
        return "success";
    }

    /**
     * 获取所有用户
     *
     * @return 用户集合
     */
    @GetMapping
    public ApiR<List<UserPo>> getAllUser() {
        return ApiR.okMsgAndData("端口：" + port + "，查询所有用户成功", userService.list());
    }

    /**
     * 根据 id 获取用户
     *
     * @param id id
     * @return 用户
     */
    @GetMapping("/{id}")
    public ApiR<UserPo> getUserById(@PathVariable String id) {
        return ApiR.okMsgAndData("端口：" + port + "，查询单个用户成功", userService.getById(id));
    }

    /**
     * 保存用户
     *
     * @param userPo 用户信息
     * @return 返回ApiR<Boolean>
     */
    @PostMapping()
    public ApiR<Boolean> saveUser(@RequestBody UserPo userPo) {
        boolean flag = userService.saveUser(userPo);
        return ApiR.msg(flag, "端口：" + port + "，保存用户成功", "端口：" + port + "，保存用户失败");
    }

    /**
     * 根据 id 修改用户
     *
     * @param userPo 用户信息
     * @return ApiR<Boolean>
     */
    @PutMapping()
    public ApiR<Boolean> edit(@RequestBody UserPo userPo) {
        Boolean flag = userService.updateUser(userPo);
        return ApiR.msg(flag, "端口：" + port + "，修改用户成功", "端口：" + port + "，修改用户失败");
    }

    /**
     * 根据id删除用户
     *
     * @param id id
     * @return ApiR<Boolean>
     */
    @DeleteMapping("/{id}")
        public ApiR<Boolean> delete(@PathVariable String id) {
        boolean flag = userService.removeById(id);
        return ApiR.msg(flag, "端口：" + port + "，删除用户成功", "端口：" + port + "，删除用户失败");
    }

}
